package edu.suda.basic;

import java.security.*;

public class MessageDigestTest
{
	public static String bytesToHex(byte[] b)
	{
		char hexDigit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
		StringBuffer buf = new StringBuffer();
		for (int j = 0; j < b.length; j++)
		{
			buf.append(hexDigit[(b[j] >> 4) & 0x0f]);
			buf.append(hexDigit[b[j] & 0x0f]);
		}
		return buf.toString();
	}
	
	public static void main(String[] a)
	{
		try
		{
			String input="";
			byte[] output=null;
			
			MessageDigest md = MessageDigest.getInstance("SHA1");
			System.out.println("Message digest object info: ");
			System.out.println("   Algorithm = " + md.getAlgorithm());
			System.out.println("   Provider = " + md.getProvider());
			System.out.println("   toString = " + md.toString());

			input = "";
			md.update(input.getBytes());
			output = md.digest();
			System.out.println();
			System.out.println("MD5(\"" + input + "\") =");
			System.out.println("   " + bytesToHex(output));

			input = "abc";
			md.update(input.getBytes());
			output = md.digest();
			System.out.println();
			System.out.println("MD5(\"" + input + "\") =");
			System.out.println("   " + bytesToHex(output));

			input = "abcdefghijklmnopqrstuvwxyz";
			md.update(input.getBytes());
			output = md.digest();
			System.out.println();
			System.out.println("MD5(\"" + input + "\") =");
			System.out.println("   " + bytesToHex(output));
		}
		catch (Exception e)
		{
			System.out.println("Exception: " + e);
		}
	}


}
